//
//  MSMytradeViewController.m
//  SocietySale
//
//  Created by yumingwei on 2016/11/26.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSMytradeViewController.h"
#import "MSMytradeTableVC.h"
#import "MSTradeTableViewController.h"
#import "MyTradeConfirmViewController.h"
#import "MSTradeCommentViewController.h"

@interface MSMytradeViewController ()
//选择条
@property (nonatomic, strong) HDMenuView          *menu;
@property (nonatomic, strong) MSMytradeTableVC    *saleTableVC;
@property (nonatomic, strong) MSMytradeTableVC    *buyTableVC;
@property (nonatomic, strong) UIScrollView        *scrollView;

@end

@implementation MSMytradeViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"我的交易";
}

- (void)initUI{
    self.menu = [[HDMenuView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) titleArray:@[@"我的出售（0）",@"我的购买（0）"]];
    self.menu.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    self.menu.normalColor = [UIColor blackColor];
    self.menu.bottomLineColor = kBlueColor;
    self.menu.delegate = (id)self;
    self.menu.isHiddenGrayLine = NO;
    [self.contentView addSubview:self.menu];

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-40)];
    _scrollView.contentSize = CGSizeMake(2*SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-40);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = (id)self;
    _scrollView.scrollsToTop = NO;
    [self.contentView addSubview:_scrollView];

    self.saleTableVC = [[MSMytradeTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.saleTableVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-40);
    self.saleTableVC.enableRefresh = YES;
    self.saleTableVC.enableRequest = YES;
    self.saleTableVC.delegate = (id)self;
    self.saleTableVC.isSale = YES;
    self.saleTableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_scrollView addSubview:self.saleTableVC.view];
    
    self.buyTableVC = [[MSMytradeTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.buyTableVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-40);
    self.buyTableVC.enableRefresh = YES;
    self.buyTableVC.enableRequest = YES;
    self.buyTableVC.delegate = (id)self;
    self.buyTableVC.isSale = NO;
    self.buyTableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_scrollView addSubview:self.buyTableVC.view];
}

- (void)testDataFunc{
    MSTradeModel *model = [[MSTradeModel alloc] init];
    model.title = @"上海浦东世和空港中心商业住房项目";
    model.projectType = @"求购业主联系人信息";
    model.tradeState = @"已出售";
    model.tradeNum = @"3次交易";
    model.name = @"周世鹏";
    model.position = @"经理";
    model.theme = @"电缆附件招标最新进展";
    model.content = @"杭州地铁二号线目前极影施工阶段";
    model.tradeEvaluate = @"评价              ";
    model.tradePrice = @"12";
    model.memberType = @"[匿名会员]";
    model.memberLevel = @"2钻会员";
    model.time = @"2016.9.15";
    model.isEvaluate = YES;
    
    MSTradeModel *model1 = [[MSTradeModel alloc] init];
    model1.title = @"上海浦东世和空港中心商业住房项目";
    model1.projectType = @"出售进展信息";
    model1.tradeState = @"已过期";
    model1.tradeNum = @"5次交易";
    model1.name = @"王鹏辉";
    model1.position = @"主管";
    //    model1.theme = @"电缆附件招标最新进展";
    //    model1.content = @"杭州地铁二号线目前极影施工阶段";
    //    model1.tradeEvaluate = @"评价              ";
    model1.tradePrice = @"23";
    model1.memberType = @"[系统录入]";
    model1.memberLevel = @"3";
    model1.time = @"2016.9.15";
    model.isEvaluate = NO;
    
    MSTradeModel *model2 = [[MSTradeModel alloc] init];
    model2.title = @"上海浦东世和空港中心商业住房项目";
    model2.projectType = @"求购设计师项目联系人信息";
    model2.tradeState = @"公开显示";
    model2.tradeNum = @"23次交易";
    //    model2.name = @"李光成";
    model2.position = @"总监";
    model2.theme = @"电缆附件招标最新进展";
    model2.content = @"杭州地铁二号线目前极影施工阶段";
    model2.tradeEvaluate = @"评价              ";
    model2.tradePrice = @"45";
    model2.memberType = @"[匿名会员]";
    model2.memberLevel = @"1";
    model2.time = @"2016.9.15";
    model.isEvaluate = YES;
    
//    self.saleTableVC.dataArray = [@[model,model1,model,model1,model,model1,model2] mutableCopy];
//    self.buyTableVC.dataArray = [@[model2,model1,model,model2,model,model1] mutableCopy];
    
    self.saleTableVC.pageNum = 1;
    [self requestSale:NO];
    self.buyTableVC.pageNum = 1;
    [self requestBuy:NO];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"SaleList"]!=nil){
            if (responseDic[@"state"]!=nil){
                self.saleTableVC.totalPagesNum = [responseDic[@"totalPage"] intValue];
                if (self.saleTableVC.pageNum == 1) {
                    self.saleTableVC.dataArray = nil;
                }
                NSMutableArray *dataArrray = [[NSMutableArray alloc] initWithArray:self.saleTableVC.dataArray];
                [dataArrray addObjectsFromArray:responseDic[@"SaleList"]];
                self.saleTableVC.dataArray = dataArrray;
                NSString *saleBtnStr = [NSString stringWithFormat:@"我的出售（%lu）",(unsigned long)dataArrray.count];
                [self.menu setTitleWithTitle:saleBtnStr AtIndex:0];
            }
            else{
                self.saleTableVC.dataArray = self.saleTableVC.dataArray;
            }
        }
        if (responseDic[@"BuyList"]!=nil){
            if (responseDic[@"state"]!=nil){
                self.buyTableVC.totalPagesNum = [responseDic[@"totalPage"] intValue];
                if (self.buyTableVC.pageNum == 1) {
                    self.buyTableVC.dataArray = nil;
                }
                NSMutableArray *dataArrray = [[NSMutableArray alloc] initWithArray:self.buyTableVC.dataArray];
                [dataArrray addObjectsFromArray:responseDic[@"BuyList"]];
                self.buyTableVC.dataArray = dataArrray;
                NSString *buyBtnStr = [NSString stringWithFormat:@"我的购买（%lu）",(unsigned long)dataArrray.count];
                [self.menu setTitleWithTitle:buyBtnStr AtIndex:1];
            }
            else{
                self.buyTableVC.dataArray = self.buyTableVC.dataArray;
            }
        }
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (self.menu.selectedIndex == 0) {
//        MyTradeConfirmViewController *vc = [[MyTradeConfirmViewController alloc] init];
//        vc.tradeType = TradeType_ContactsPurchase;
//        [rootNavigation pushViewController:vc animated:YES];
//    }
//    else{
//        MyTradeConfirmViewController *vc = [[MyTradeConfirmViewController alloc] init];
//        vc.tradeType = TradeType_StagePurchase;
//        [rootNavigation pushViewController:vc animated:YES];
//    }
}

#pragma mark DYTableViewProtocol
/** 上拉 */
- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage{
    if (self.menu.selectedIndex == 0) {
        self.saleTableVC.pageNum++;
        [self requestSale:YES];
    }
    if (self.menu.selectedIndex == 1) {
        self.buyTableVC.pageNum++;
        [self requestBuy:YES];
    }
}

- (void)pullRefreshRequest
{
    if (self.menu.selectedIndex == 0) {
        self.saleTableVC.pageNum = 1;
        [self requestSale:YES];
    }
    if (self.menu.selectedIndex == 1) {
        self.buyTableVC.pageNum = 1;
        [self requestBuy:YES];
    }
}

-(void)requestSale:(BOOL)isPull
{
    UIView * hudView = isPull?nil:self.contentView;
    NSString *pageNumber = [NSString stringWithFormat:@"%d",self.saleTableVC.pageNum];
    NSDictionary *dict = @{
                           @"pageNo":pageNumber,
                           @"pageSize":@"10",
                           };
    [netEngine requestSaleListWithTarget:self.VctrID hudView:hudView params:dict];
}


-(void)requestBuy:(BOOL)isPull
{
    UIView * hudView = isPull?nil:self.contentView;
    NSString *pageNumber = [NSString stringWithFormat:@"%d",self.buyTableVC.pageNum];
    NSDictionary *dict = @{
                           @"pageNo":pageNumber,
                           @"pageSize":@"10",
                           };
    [netEngine requestBuyListWithTarget:self.VctrID hudView:hudView params:dict];
}

- (void)clickCommonBtnIsEdit:(BOOL)isEdit orderId:(NSString*)ordertId{
    if (isEdit) {
//        MyTradeConfirmViewController *vc = [[MyTradeConfirmViewController alloc] init];
//        vc.tradeType = TradeType_ContactsPurchase;
//        [rootNavigation pushViewController:vc animated:YES];
    }
    else{
        MSTradeCommentViewController *vc = [[MSTradeCommentViewController alloc] init];
        vc.isComment = YES;
        vc.orderId = ordertId;
        [rootNavigation pushViewController:vc animated:YES];
    }
}

#pragma mark - 顶部选择器代理
- (void)selectMenuIndex:(int)index{
    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH*index, 0);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.menu.selectedIndex = scrollView.contentOffset.x/SCREEN_WIDTH;
}

@end
